<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/10/05 23:56:16 $)
// $RCSfile: client_news.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/news/functions_clientnews.php");
require_once ("./includes/functions_users.php");
require_once ("./includes/functions_comments.php");
$template->loadLanguageSection("newsclient");

// ======= PERMISSION CHECK =======
if ($_SWIFT["user"]["permissions"]["perm_canviewnews"] == "0" || $_SWIFT["settings"]["nw_enableclient"] == "0")
{
	$template->assign("errormessage", $_SWIFT["language"]["perminvalid"]);
	echo $template->displayTemplate("header");
	echo $template->displayTemplate("footer");
	exit;
}

/**
* ###############################################
* VIEW NEWS LIST
* ###############################################
*/
if ($eventaction == "view")
{
	$template->cache(array('nwlistdisplay', 'nwnavbar', 'header', 'footer', 'navbar'));

	if (empty($_REQUEST["offset"]))
	{
		$_offset = 0;
	} else {
		$_offset = intval($_REQUEST["offset"]);
	}
	$_limit = intval($_SWIFT["settings"]["nw_pageno"]);
	if (empty($_limit))
	{
		$_limit = 1;
	}

	$newslist = getClientNews($_GET["filter"], $_offset, $_limit);
	$template->assign("incnwnav", true);
	$template->assign("newslist", $newslist);
	$template->assign("filter", $_GET["filter"]);

	$template->assign("navigation", '<a href="'. iif(defined("ISMODERNBILL"), "user.php", "index.php") .'" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="'. iif(defined("ISMODERNBILL"), "user.php?op=menu&tile=mynews_tab&_m=news&_a=view", "index.php?_m=news&_a=view") .'" id="navlink">'.$_SWIFT["language"]["news"]."</a>");

	$template->assign("sectiontitle", $_SWIFT["language"]["news"]);
	$template->assign("sectiondesc", $_SWIFT["language"]["nwviewdesc"]);

	if ($_offset == 0)
	{
		$template->assign("backurl", iif(defined("ISMODERNBILL"), "user.php", "index.php"));
	}

	if ($_GET["subscriberesult"] == 2)
	{
		$template->assign("infomessage", $_SWIFT["language"]["nwsubvalidateconfirm"]);
	} else if ($_GET["subscriberesult"] == 3) {
		$template->assign("errormessage", $_SWIFT["language"]["nwvalidhasherror"]);
	} else if ($_GET["subscriberesult"] == 1 && $_SWIFT["settings"]["nw_svalidate"] == 1) {
		$template->assign("infomessage", $_SWIFT["language"]["nwsubemailconfirm"]);
	} else if ($_GET["subscriberesult"] == 1 && $_SWIFT["settings"]["nw_svalidate"] != 1) {
		$template->assign("infomessage", $_SWIFT["language"]["nwsubconfirm"]);
	} else if ($_GET["subscriberesult"] == "-1") {
		$template->assign("errormessage", $_SWIFT["language"]["nwsuberror"]);
	} else if ($_GET["unsubscriberesult"] == "1") {
		$template->assign("infomessage", $_SWIFT["language"]["nwunsubconfirm"]);
	}

	echo $template->displayTemplate("nwlistdisplay");




/**
* ###############################################
* 
* ###############################################
*/
} else if ($eventaction == "viewnews") {
	$template->cache(array('nwviewnews', 'nwitemnavbar', 'header', 'footer', 'navbar'));

	$_news = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."news` AS news LEFT JOIN `". TABLE_PREFIX ."newsdata` AS newsdata ON (news.newsid = newsdata.newsid) WHERE news.newsid = '". intval($_REQUEST["newsid"]) ."';");
	if ($_news["tgroupid"] != $_SWIFT["tgroup"]["tgroupid"] || ($_news["expiry"] < DATENOW && $_news["expiry"] != 0))
	{
		trigger_error($_SWIFT["language"]["nwinvalidnewsitem"], E_USER_ERROR);
	}

	$template->assign("navigation", '<a href="'. iif(defined("ISMODERNBILL"), "user.php", "index.php") .'" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="'. iif(defined("ISMODERNBILL"), "user.php?op=menu&tile=mynews_tab&_m=news&_a=view", "index.php?_m=news&_a=view") .'" id="navlink">'.$_SWIFT["language"]["news"].'</a> &raquo; <a href="'. iif(defined("ISMODERNBILL"), 'user.php?op=menu&tile=mynews_tab_m=news&_a=viewnews&newsid='. intval($_news["newsid"]), 'index.php?_m=news&_a=viewnews&newsid='. intval($_news["newsid"])) .'" id="navlink">'.htmlspecialchars($_news["subject"]).'</a>');

	renderClientComments(COMMENT_NEWS, $_news["newsid"]);

	$_news["contents"] = getProcessedHTML($_news["contents"]);
	$_news["description"] = getProcessedHTML($_news["description"]);
	$_news["fullname"] = $_SWIFT["staffcache"][$_news["staffid"]]["fullname"];
	$_news["dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $_news["dateline"]);
	$_news["jssubject"] = addslashes($_news["subject"]);

	$template->assign("backurl", iif(defined("ISMODERNBILL"), "user.php?op=menu&tile=mynews_tab&_m=news&_a=view", "index.php?_m=news&_a=view"));
	$template->assign("news", $_news);
	$template->assign("nwitemnav", true);

	echo $template->displayTemplate("nwviewnews");






/**
* ###############################################
* SUBSCRIBE
* ###############################################
*/
} else if ($eventaction == "subscribe") {
	require_once ("./modules/news/functions_subscribers.php");

	if (!empty($_GET["hash"]))
	{
		// Validate
		$_hash = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."newsverihash` WHERE `hash` = '". $dbCore->escape($_GET["hash"]) ."';");
		if ($_hash["hash"] == $_GET["hash"] && trim($_GET["hash"]) != "")
		{
			validateSubscriber($_hash["newssubscriberid"]);

			header("location: index.php?_m=news&_a=view&subscriberesult=2");
		} else {
			header("location: index.php?_m=news&_a=view&subscriberesult=3");
		}

	} else {
		// See if this user is already subscribed
		$_subscriber = $dbCore->queryFetch("SELECT `email` FROM `". TABLE_PREFIX ."newssubscribers` WHERE `email` = '". $dbCore->escape($_POST["email"]) ."';");
		if (empty($_subscriber["email"]) && isValidEmail($_POST["email"]))
		{
			insertSubscriber($_POST["email"], iif($_SWIFT["settings"]["nw_svalidate"]==1, false, true), $_SWIFT["user"]["userid"], $_SWIFT["tgroup"]["tgroupid"]);

			header("location: index.php?_m=news&_a=view&subscriberesult=1");
		} else {
			header("location: index.php?_m=news&_a=view&subscriberesult=-1");
		}
	}



/**
* ###############################################
* UNSUBSCRIBE
* ###############################################
*/
} else if ($eventaction == "unsubscribe") {
	require_once ("./modules/news/functions_subscribers.php");

	$_subscriber = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."newssubscribers` WHERE `email` = '". $dbCore->escape($_GET["email"]) ."';");
	if (!empty($_subscriber["newssubscriberid"]))
	{
		deleteSubscribers(array($_subscriber["newssubscriberid"]));
	}

	header("location: index.php?_m=news&_a=view&unsubscriberesult=1");




/**
* ###############################################
* PDF EXPORT
* ###############################################
*/
} else if ($eventaction == "pdfexport") {
	require_once ("./modules/news/functions_news.php");

	makeNewsPDF($_GET["newsid"], false);
}



?>